package com.qububu.ad.control;

import static com.qububu.utils.ParamConverter.assertNotEmpty;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qububu.ad.dto.TweetReply;
import com.qububu.ad.dto.TweetInfo;
import com.qububu.ad.dto.TweetInfo.TweetStatus;
import com.qububu.ad.service.TweetService;
import com.qububu.common.user.dto.UserInfoDto;
import com.qububu.utils.DateUtil;
import com.qububu.utils.SystemConstants;
import com.qububu.utils.SystemUtil;

@RequestMapping("/ad")
@Controller
public class TweetControl {

	@Resource
	private TweetService tweetService;
	
	/**
	 * 进入广告管理页面
	 * */
	@RequestMapping("/admin/infoAdminAd")
	public String infoAdminAd() {
		
		return "adminAd";
	}
	
	/**
	 * 进入发布推文的界面
	 * */
	@RequestMapping("/admin/publishTweet")
	public String publishTweet() {
		
		return "adminAddTweet";
	}
	
	/**
	 * 添加推文
	 * */
	@RequestMapping("/admin/doPublishTweet")
	public String doPublishTweet(HttpServletRequest req) {
		String title = SystemUtil.removeAllScriptFromHtml(assertNotEmpty(req.getParameter("title")));
		String content = SystemUtil.removeAllScriptFromHtml(assertNotEmpty(req.getParameter("content")));
		UserInfoDto admin = (UserInfoDto) req.getSession().getAttribute(SystemConstants.LOGINUSER_SESSION_NAME);
		TweetInfo tweet = new TweetInfo(SystemUtil.getUUIDString(), admin.getUserId());
		tweet.setContent(content);
		tweet.setTitle(title);
		tweet.setCreateDttm(DateUtil.convertCurrentDTTMtoInt());
		tweet.setCreateIp(SystemUtil.getRequestIp(req));
		tweet.setTweetStatus(TweetStatus.NORMAL);
		tweetService.doAddTweet(tweet);
		
		return "redirect:/ad/showTodayTweet";
	}
	
	@RequestMapping("/showTodayTweet")
	public String showTodayTweet(HttpServletRequest req) {
		TweetInfo tweet = tweetService.getTodayTweet();
		
		req.setAttribute("tweet", tweet);
		return "phoneTweets";
	}
	
	/**
	 * 根据tweetId显示一篇推文
	 * */
	@RequestMapping("/showTweet")
	public String showTweet(HttpServletRequest req) {
		String tweetId = assertNotEmpty(req.getParameter("t"));
		TweetInfo tweet = tweetService.getTweet(tweetId);
		if (tweet == null) {
			String preUrl = req.getHeader("Referer");
			return "redirect:"+preUrl;
		}
		req.setAttribute("tweet", tweet);
		return "phoneTweets";
	}
	
	@RequestMapping("/addTweetReply")
	@ResponseBody
	public Map<String, Object> addTweetReply(HttpServletRequest req) {
		String tweetId = assertNotEmpty(req.getParameter("ti"));
		String content = SystemUtil.removeAllScriptFromHtml(assertNotEmpty(req.getParameter("content")));
		String authorName = req.getSession().getAttribute(SystemConstants.LOGINUSER_SESSION_NAME) != null ? ((UserInfoDto) req.getSession().getAttribute(SystemConstants.LOGINUSER_SESSION_NAME)).getNickName() : "游客";
		TweetReply reply = new TweetReply(SystemUtil.getUUIDString());
		reply.setAuthorName(authorName);
		reply.setContent(content);
		reply.setCreateDttm(DateUtil.convertCurrentDTTMtoInt());
		reply.setCreateIp(SystemUtil.getRequestIp(req));
		tweetService.addTweetReply(reply, tweetId);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		
		return resultMap;
	}
	
	@RequestMapping("/getTweetReplys")
	@ResponseBody
	public Map<String, Object> getTweetApplys(HttpServletRequest req) {
		String tweetId = assertNotEmpty(req.getParameter("ti"));
		List<TweetReply> replyList = tweetService.getTweetReplysByTweetId(tweetId);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		resultMap.put("replys", replyList);
		
		return resultMap;
	}
	
	/**
	 * 跳转到推文管理页面
	 * */
	@RequestMapping("/admin/tweetConsole")
	public String tweetConsole(HttpServletRequest req) {
		req.setAttribute("tweets", tweetService.getTweets());
		return "adminTweets";
	}
	
	/**
	 * 改变推文状态
	 * */
	@RequestMapping("/admin/changeTweetStatus")
	@ResponseBody
	public Map<String, Object> changeTweetStatus(HttpServletRequest req) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String tweetId = assertNotEmpty(req.getParameter("tweetId"));
		String status = assertNotEmpty(req.getParameter("status"));
		tweetService.changeTweetStatus(tweetId, TweetStatus.getTweetStatus(status).toIntValue());
		resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		return resultMap;
	}
	
	/**
	 * 删除推文及其对应的评论
	 * */
	@RequestMapping("/admin/deleteTweet")
	@ResponseBody
	public Map<String, Object> deleteTweet(HttpServletRequest req) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String tweetId = assertNotEmpty(req.getParameter("tweetId"));
		tweetService.doDeleteTweet(tweetId);
		resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		return resultMap;
	}
}
